package com.scfinance.SPD.service;

import com.scfinance.SPD.dao.OrderDaoImpl;
import com.scfinance.SPD.encrypt.util.Constant;
import com.scfinance.SPD.externalReq.SPDBankPAD;
import com.scfinance.SPD.util.Constants;
import com.scfinance.SPD.util.FileUtil;
import com.scfinance.SPD.util.PropsUtil;
import com.scfinance.SPD.util.ZipUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2018/1/9.
 */
@Service
public class SPDImgService extends SPDSimpleService {

    private Logger logger = LoggerFactory.getLogger(SPDImgService.class);

    public static final String UPLOADIMAGE_URL = "uploadImage.fileUrl";

    @Autowired
    private OrderDaoImpl orderDao;

    public void upImgAlone(String filePath) {
        String[] strList = filePath.split(",");
        String path = strList[0];
        String loanOrderId = strList[1];
        Object[] params = {loanOrderId};
        List<Map<String, Object>> listMap = orderDao.getCustomerAppByOrderId(params);
        logger.info("需要单独上传图片的订单 " + loanOrderId);
        for (Map<String, Object> map : listMap) {
            String imgReturn = upImageAlone(path,map);
            if ("ok".equals(imgReturn) || "repeat".equals(imgReturn)) {
                Object[] params1 = {Constants.SPD_SEND_SUCCESS, loanOrderId};     //发送成功
                updateSingleFlag(params1);
                saveOperationRecord(map, "2000", "导出浦发");
                pushSPDEntry(loanOrderId);  //发送推送
                setSPDOutputSuccess(loanOrderId);
            } else {
                logger.info("订单" + loanOrderId + "图片上传失败");
                System.out.println("订单" + loanOrderId + "图片上传失败");
                Object[] params2 = {Constants.SPD_IMAGE_UPLOAD_FAIL, loanOrderId};     //图片上传失败
                updateSingleFlag(params2);
                saveOperationRecord(map, "2001", "导出浦发失败");
                setSPDOutputDefault(loanOrderId);
            }
        }
    }

    private String upImageAlone(String path, Map<String, Object> map) {
        String returnFlag = null;
        String loanOrderId = (String) map.get("order_id");
        String appId = (String) map.get("appId");
        try {
            String zipName = ZipUtil.buildZipWithPath(path, map);
            returnFlag = SPDBankPAD.uploadImg(zipName, appId);
            FileUtil.delete(zipName);
        } catch (Exception e) {
            logger.info("订单" + loanOrderId + "------打ZIP图片，出错" + e);
            Object[] params = {Constants.EXCEPTIONFLAG, loanOrderId};
            updateSingleFlag(params);
            setSPDOutputDefault(loanOrderId);
            e.printStackTrace();
        }
        return returnFlag;
    }

    public void reUpImage(String loanOrderId) throws Exception {
        Object[] params = {loanOrderId};
        List<Map<String, Object>> listMap = orderDao.getCustomerAppByOrderId(params);
        logger.info("需要重新上传图片的订单 " + loanOrderId);
        for (Map<String, Object> map : listMap) {
            String appId = (String) map.get("appId");
            String imgReturn = uploadImage(appId, map, loanOrderId);
            if ("ok".equals(imgReturn) || "repeat".equals(imgReturn)) {
                Object[] params1 = {Constants.SPD_SEND_SUCCESS, loanOrderId};     //发送成功
                updateSingleFlag(params1);
                setSPDOutputSuccess(loanOrderId); //设置导出成功
                saveOperationRecord(map, "2000", "导出浦发");
                pushSPDEntry(loanOrderId);  //发送推送
            } else {
                logger.info("订单" + loanOrderId + "图片上传失败");
                System.out.println("订单" + loanOrderId + "图片上传失败");
                Object[] params2 = {Constants.SPD_IMAGE_UPLOAD_FAIL, loanOrderId};     //图片上传失败
                updateSingleFlag(params2);
                setSPDOutputDefault(loanOrderId); //设置导出失败
                saveOperationRecord(map, "2001", "导出浦发失败");
            }
        }
    }

    public String uploadImage(String appId, Map<String, Object> map, String loanOrderId) {
        String returnFlag = null;
        try {
            String zipName = ZipUtil.buildZip(appId, map);
            logger.info("订单" + loanOrderId + "------上传图片，发送" + zipName);
            System.out.println("订单" + loanOrderId + "------上传图片，发送" + zipName);
            returnFlag = SPDBankPAD.uploadImg(zipName, appId);
            FileUtil.delete(zipName);
            if ("ok".equals(returnFlag) || "repeat".equals(returnFlag)){
                logger.info("订单" + loanOrderId + "删除备份图片");
                FileUtil.delete(PropsUtil.getProperty(UPLOADIMAGE_URL) + loanOrderId + "backUp");
            }
            logger.info("订单" + loanOrderId + "------上传图片，返回" + returnFlag);
            System.out.println("订单" + loanOrderId + "------上传图片，返回" + returnFlag);
        } catch (Exception e) {
            logger.info("订单" + loanOrderId + "------上传图片，出错" + e);
            Object[] params = {Constants.EXCEPTIONFLAG, loanOrderId};     //Exception 状态
            updateSingleFlag(params);
            setSPDOutputDefault(loanOrderId);
            e.printStackTrace();
        }
        return returnFlag;
    }
}
